package com.ibm.cps.sql;

import com.ibm.util.ErrorCode;
import com.ibm.util.exception.CPSException;

/**
 * Created by telekinesis on 6/8/15.
 */
public class DftDescriptor {
    private static final int DEFAULT_COEFFICIENT = 2;

    private final String column;
    private final int coefficientsToOutput;

    private DftDescriptor(String column, int coefficientsToOutput){
        this.column = column;
        this.coefficientsToOutput = coefficientsToOutput;
    }

    public String getColumn() {
        return column;
    }

    public int getCoefficientsToOutput() {
        return coefficientsToOutput;
    }

    public static DftDescriptor parseFromString(String arguments) throws CPSException{
        arguments = arguments.toLowerCase();
        String[] args = arguments.split(",");
        if(args.length == 1){
            return new DftDescriptor(args[0], DEFAULT_COEFFICIENT);
        }else if(args.length > 2){
            throw new CPSException(ErrorCode.PARSE_ERROR_CODE, "illegal argument list of dft function, only 2 arguments (attribute name and coefficient) allowed");
        }else{
            try {
                String attribute = args[0];
                int coefficient = Integer.parseInt(args[1]);
                return new DftDescriptor(attribute, coefficient);
            }catch(RuntimeException e){
                throw new CPSException(ErrorCode.PARSE_ERROR_CODE, "illegal dft coefficient, should be double value");
            }
        }
    }

    @Override
    public String toString() {
        return "DftDescriptor{" +
                "coefficient=" + coefficientsToOutput +
                ", column='" + column + '\'' +
                '}';
    }
}
